//
//  HRSignViewController.m
//  Hear
//
//  Created by mac on 2018/3/5.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRSignViewController.h"
#import "HRSignHeadView.h"
#import "HRSignListCell.h"
#import "HRShowSignResultView.h"
#import "HRSignViewModel.h"
#import "HRSignInModel.h"
#import "HRRecommendFriendsViewController.h"

@interface HRSignViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)HRSignHeadView *headView;

@property (nonatomic,strong)UIView *footView;

@property (nonatomic,strong)HRSignViewModel *viewModel;

@property (nonatomic,strong)HRSignInModel *signInModel;

@end

@implementation HRSignViewController
#pragma mark - Get and Set
- (HRSignViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRSignViewModel alloc] init];
    }
    return _viewModel;
}

- (UIView *)footView{
    if (_footView == nil) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 80)];
        UILabel *showTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, _footView.width, 20)];
        showTextLabel.text = @"您所获得的乐币可以累积，用于消费和提现";
        showTextLabel.textAlignment = 1;
        showTextLabel.font = CustomUIFont(13);
        showTextLabel.textColor = BlueColor;
        [_footView addSubview:showTextLabel];
        
    }
    return _footView;
}

- (HRSignHeadView *)headView{
    if (_headView == nil) {
        WeakSelf
        _headView = [[HRSignHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height/2+60)];
        _headView.signBlock = ^{
            [weakSelf signIn];
        };
    }
    return _headView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    

    [self setupView];
    
    if (@available(iOS 11.0, *))
    {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedRowHeight = 0;
    }
    else
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    // Do any additional setup after loading the view.
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.signInModel.taskInfoModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HRSignListCell *cell = [HRSignListCell cellWithTableView:tableView];
    cell.signModel = self.signInModel.taskInfoModelArr[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HRSignInModel *signModel =self.signInModel.taskInfoModelArr[indexPath.row];
    if ([signModel.type intValue] == 1 || [signModel.type intValue] == 2){
        self.tabBarController.selectedIndex = 2;
    }else if ([signModel.type intValue] == 3){
        HRRecommendFriendsViewController *VC = [HRRecommendFriendsViewController new];
        VC.hearIdStr = self.userInfoModel.UserNo;
        VC.coins = signModel.coins;
        [self.navigationController pushViewController:VC animated:YES];
    }
}

- (void)getContentInfo{
    WeakSelf
    [self.viewModel getDailyMission:@{@"userLocalId":USERID} isSeccess:^(HRSignInModel *signInModel) {
        if (signInModel){
            weakSelf.signInModel = signInModel;
            _headView.signInModel = signInModel;
            [[NSUserDefaults standardUserDefaults] setObject:weakSelf.signInModel.Balance forKey:@"Balance"];
            [weakSelf.tableView reloadData];
        }
    }];
}

- (void)signIn{
    WeakSelf
    [self.viewModel saveUserSignIn:@{@"userLocalId":USERID,@"CreateTime":[self getCurrentTime]} isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            dispatch_async(dispatch_get_main_queue(), ^{
                [HRShowSignResultView showSignResultView:@"恭喜您签到成功" andGoldNum:[NSString stringWithFormat:@"+%@",weakSelf.signInModel.signInfoModel.coins]];
            });
            weakSelf.signInModel.signInfoModel.completion = @"1";
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",[weakSelf.signInModel.Balance intValue]+[weakSelf.signInModel.signInfoModel.coins intValue]] forKey:@"Balance"];
        }
    }];
}

- (NSString *)getCurrentTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    return [formatter stringFromDate:[NSDate date]];
}

- (void)setupView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = self.headView;
    self.tableView.rowHeight = 50;
    self.tableView.tableFooterView = self.footView;
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = 1;
    [self.view  addSubview:titleLabel];
    titleLabel.frame = CGRectMake(50, 20, 100, kStatusBarHeight+kNavBarHeight-20);
    titleLabel.centerX = self.view.centerX;
    titleLabel.text = @"今日任务";
    
    UIButton *leftButton = [UIButton buttonWithType:0];
    leftButton.frame = CGRectMake(10, kStatusBarHeight, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"Back-Arrow"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
}


- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [self getContentInfo];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
